﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Runtime.InteropServices;
using EnvDTE;
using VsxFactory.Modeling.VisualStudio;
using System.Diagnostics.Contracts;
using System.Drawing.Design;
using VsxFactory.Modeling.Strategies;

namespace VsxFactory.Modeling.StrategyManager.Strategies
{
    [ClassInterface(ClassInterfaceType.AutoDispatch), ComVisible(true)]
    public class DependenceFileStrategyT4Extender : BaseStrategyExtender, INotifyPropertyChanged, IFilterProperties
    {
        internal DependenceFileStrategyT4Extender(IServiceProvider serviceProvider, IVsProjectFileItem Item) : base(serviceProvider, Item)
        {
        }

        [Browsable(true)]
        [DisplayName("Dependent file")]
        [Category("Code generation")]
        [Editor(typeof(DependentFileEditor), typeof(UITypeEditor))]
        [Description("File name of the dependent file (which must have a strategy defined).")]
        public string DependentFile
        {
            get
            {
                return this.Item.GetAttribute("MXDependentFile");
            }
            set
            {
                if (value != DependentFile)
                {
                    this.Item.SetAttribute("MXDependentFile", value);
                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("DependentFile"));
                }
            }
        }

        [Browsable(true)]
        [DisplayName("Output file format")]
        [Category("Code generation")]
        [Description("Output format name of the generated file. The format can be contains macro expression like : {property name} (only for event on an specific element) or expand variable (%system variable%)")]
        public string OutputFileFormat
        {
            get
            {
                return this.Item.GetAttribute("MXOutputFileFormat");
            }
            set
            {
                this.Item.SetAttribute("MXOutputFileFormat", value);
            }
        }

        [Browsable(true)]
        [DisplayName("Template file name")]
        [Category("Code generation")]
        [Description("Template file name. If nothing is defined, the current document must be a template.")]
        public string TemplateFileName
        {
            get
            {
                return this.Item.GetAttribute("MXTemplateFileName");
            }
            set
            {
                this.Item.SetAttribute("MXTemplateFileName", value);
            }
        }

        [Browsable(true)]
        [DisplayName("Model element name")]
        [Category("Code generation")]
        [Description("Filter on a model element name. (Display name)")]
        public string ModelElementName
        {
            get
            {
                return this.Item.GetAttribute("MXModelElementName");
            }
            set
            {
                this.Item.SetAttribute("MXModelElementName", value);
            }
        }

        [DefaultValue(CodeGenerationEvent.OnModelFileSaved)]
        [Browsable(true)]
        [DisplayName("Generation event")]
        [Category("Code generation")]
        [Editor(typeof(CodeGenerationEventEditor), typeof(System.Drawing.Design.UITypeEditor))]
        [Description("Filter on the generation event. ModelElementAdded other than OnModelFileSaved must have a specific template which can share the in memory store.")]
        public CodeGenerationEvent GenerationEvent
        {
            get
            {
                return this.Item.GetAttributeAsEnum<CodeGenerationEvent>("MXGenerationEvent", CodeGenerationEvent.None);
            }
            set
            {
                this.Item.SetAttribute("MXGenerationEvent", value.ToString());
            }
        }

        [DefaultValue(true)]
        [Browsable(true)]
        [DisplayName("Overwrite generated file")]
        [Category("Code generation")]
        [Description("True if the file is always regenerated.")]
        public bool Overwrite
        {
            get
            {
                return this.Item.GetAttributeAsBoolean("MXOverwriteFile", true);
            }
            set
            {
                this.Item.SetAttribute("MXOverwriteFile", value.ToString());
            }
        }

        vsFilterProperties IFilterProperties.IsPropertyHidden(string propertyName)
        {
            // TODO n'est jamais appelé pour les propriétés de l'extender
            switch (propertyName)
            {
                case "Overwrite" :
                case "OutputFileFormat":
                case "TemplateFileName":
                case "ModelElementName":
                case "GenerationEvent":
                    return vsFilterProperties.vsFilterPropertiesAll; // On cache la propriété
            }

            return vsFilterProperties.vsFilterPropertiesNone; // On l'affiche
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
